<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备标识" prop="deviceMark">
        <el-input
          v-model="queryParams.deviceMark"
          placeholder="请输入设备标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-input
          v-model="queryParams.deviceType"
          placeholder="请输入设备类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        > -->
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="workshopList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="设备标识" align="center" prop="deviceMark" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="检测区域" align="center" prop="regionName" />
      <el-table-column label="设备类型" align="center" prop="deviceType" />
      <el-table-column label="禁用/启用" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enableFlag"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="设备状态" align="center" prop="deviceStatus">
        <template slot-scope="scope">
          <span
            v-if="scope.row.deviceStatus == '0'"
            style="color: green; font-weight: bold"
            >在线</span
          >
          <span
            v-if="scope.row.deviceStatus == '1'"
            style="color: red; font-weight: bold"
            >离线</span
          >
        </template>
      </el-table-column>
      <el-table-column label="上次在线" align="center" prop="lastTimeOnline">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastTimeOnline) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="上次数据上报"
        align="center"
        prop="lastDataReportTime"
      />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.userId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或编辑店铺对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="20">
            <el-form-item label="设备名称" prop="deviceName">
              <el-input
                v-model="form.deviceName"
                placeholder="请输入设备名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="设备标识" prop="deviceMark">
              <el-input
                v-model="form.deviceMark"
                placeholder="请输入设备标识"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="设备类型" prop="deviceType">
              <el-input
                v-model="form.deviceType"
                placeholder="请输入设备类型"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="检测区域" prop="shopMonitorId">
              <el-select
                style="width: 100%"
                v-model="form.shopMonitorId"
                placeholder="仪器类别"
              >
                <el-option
                  v-for="item in devTypeArray"
                  :key="item.id"
                  :label="item.regionName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="设备序列号" prop="deviceSerialNumber">
              <el-input
                v-model="form.deviceSerialNumber"
                placeholder="请输入设备序列号"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="通道号" prop="channelNumber">
              <el-input
                v-model="form.channelNumber"
                placeholder="请输入通道号"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="设备IP" prop="deviceIp">
              <el-input v-model="form.deviceIp" placeholder="请输入设备IP" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  findShopDevice,
  getShopMonitorVos,
  insertShopDevice,
  updateShopDevice,
  deleteShopDevice,
} from "@/api/shop/configuration/equipment.js";

export default {
  data() {
    return {
      autoGenFlag: true,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      workshopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查看
      openQuery: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        organId: localStorage.getItem("organId"),
      },
      organId: localStorage.getItem("organId"),
      // 表单参数
      form: {},
      formQuery: {},
      devTypeArray: [],
      // 表单校验
      rules: {
        workshopCode: [
          { required: true, message: "编码不能为空", trigger: "blur" },
        ],
        workshopName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        enableFlag: [
          { required: true, message: "是否启用不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getFirstEquipment();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      findShopDevice(this.queryParams).then((response) => {
        this.workshopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getFirstEquipment() {
      const organId = this.organId;
      getShopMonitorVos(organId).then((response) => {
        this.devTypeArray = response.data || [];
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        organId: localStorage.getItem("organId"),
        deviceMark: "",
        deviceName: "",
        deviceType: "",
        enableFlag: "0",
        lastDataReportTime: null,
        lastTimeOnline: null,
        regionName: "",
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("form");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.workshopId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.enableFlag === "0" ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.deviceName + '"用户吗？')
        .then(function () {
          return updateShopDevice({
            id: row.id,
            enableFlag: row.enableFlag,
            deviceStatus: row.deviceStatus === "0" ? "1" : "0",
          });
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
          this.getList();
        })
        .catch(function () {
          row.enableFlag = row.enableFlag === "0" ? "1" : "0";
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增设备";
      this.optType = "add";
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.reset();
      this.form = row;
      this.form.shopMonitorId = Number(row.shopMonitorId)
      this.open = true;
      this.title = "编辑设备";
      this.optType = "edit";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workshopIds = row.id || this.id;
      deleteShopDevice({ id: workshopIds }).then((response) => {
        this.$modal
          .confirm('是否确认删除设备名称为"' + row.deviceName + '"的数据项？')
          .then(() => {
            this.$modal.msgSuccess("删除成功");
            this.open = false;
            this.getList();
          })
          .catch(() => {});
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateShopDevice(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertShopDevice(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
